package cn.kailangtu.myapplication.tools;

import android.content.Context;
import android.media.MediaPlayer;
import android.os.SystemClock;
import android.util.Log;

import cn.kailangtu.myapplication.MyApplication;
import cn.kailangtu.myapplication.R;


/**
 * Created by fujf on 2019/6/24
 * Description: 音频文件播放
 */
public class MediaPlayerUtils {
    private static MediaPlayerUtils mMediaPlayerUtils;
    private MediaPlayer mMediaPlayer,powerMediaPlayer;
    private Context mContext;

    /*
     * 单例模式创建
     * */
    public static MediaPlayerUtils getInstance() {
        if (mMediaPlayerUtils == null) {
            mMediaPlayerUtils = new MediaPlayerUtils();
        }
        return mMediaPlayerUtils;
    }
    private MediaPlayerUtils() {
        mContext = MyApplication.ApplicationContext;
    }

    public void playPower30(){
        try {
            if(powerMediaPlayer != null)
            {
                powerMediaPlayer.release();
            }
            powerMediaPlayer  = MediaPlayer.create(mContext, R.raw.power_p30);
            powerMediaPlayer.start();
        } catch (Exception e) {
            //L.smEE("播放开门成功语音异常：" + e.toString());
        }
    }


    public void playPower20(){
        try {

            if(powerMediaPlayer != null)
            {
                powerMediaPlayer.release();
            }
            powerMediaPlayer = MediaPlayer.create(mContext, R.raw.power_p20);
            powerMediaPlayer.start();
        } catch (Exception e) {
            //L.smEE("播放开门成功语音异常：" + e.toString());
        }
    }

    public void playPowerLow(){
        try {

            if(powerMediaPlayer != null)
            {
                powerMediaPlayer.release();
            }
            powerMediaPlayer = MediaPlayer.create(mContext, R.raw.power_low);
            powerMediaPlayer.start();
        } catch (Exception e) {
            //L.smEE("播放开门成功语音异常：" + e.toString());
        }
    }

    String TAG = "MediaPlayerUtils";
    public void playZhuanxiang(){
        try {
            Log.d(TAG,"playZhuanxiang,1");
            if(mMediaPlayer != null)
            {
                mMediaPlayer.release();
            }
             mMediaPlayer = MediaPlayer.create(mContext, R.raw.zhuanxiang);
            mMediaPlayer.start();
        } catch (Exception e) {
            Log.e(TAG,"播放开门成功语音异常：" + e.toString());
        }
    }

    boolean isZhuanxiang = false;

    public void playZhuanxiangStart(){
        if(!isZhuanxiang) {
            isZhuanxiang = true;
            new Thread(new Runnable() {
                @Override
                public void run() {
                    int index = 0;

                    Log.d(TAG, "playZhuanxiangStart,1");

                    while (isZhuanxiang) {
                        playZhuanxiang();
                        SystemClock.sleep(4200);
                    }

                }
            }).start();
        }

    }

    public void playZhuanxiangEnd(){
        isZhuanxiang = false;
        if(mMediaPlayer != null)
        {
            mMediaPlayer.release();
        }
    }



    public void playZhuanxiang1(){
        try {
            Log.d(TAG,"playZhuanxiang,1");
            if(mMediaPlayer != null)
            {
                mMediaPlayer.release();
            }
            mMediaPlayer = MediaPlayer.create(mContext, R.raw.output);
            mMediaPlayer.start();
        } catch (Exception e) {
            Log.e(TAG,"播放开门成功语音异常：" + e.toString());
        }
    }
    public void playZhuanxiang2(){
        try {
            Log.d(TAG,"playZhuanxiang,1");
            if(mMediaPlayer != null)
            {
                mMediaPlayer.release();
            }
            mMediaPlayer = MediaPlayer.create(mContext, R.raw.output1);
            mMediaPlayer.start();
        } catch (Exception e) {
            Log.e(TAG,"播放开门成功语音异常：" + e.toString());
        }
    }

}
